<!-- 作业地址 -->
<template>
	<view class="page">
		<view class="map-box">
			<map id="map" class="map" :latitude="location.latitude" :longitude="location.longitude"
				:show-location="true" @regionchange="regionchange">
			</map>
			<view class="mark">
				<text class="label" v-if="address">{{address}}</text>
				<image class="location-icon" src="/static/images/location-fill.svg" mode="aspectFit">
				</image>
			</view>
		</view>

		<view class="btn-box">
			<button class="submit-btn btn-transition-scale95" @click="handleSave()">确定</button>
		</view>
	</view>
</template>

<script setup>
	import api from '@/api/index.js';
	import { onLoad } from '@dcloudio/uni-app';
	import { ref, onMounted, getCurrentInstance } from "vue";

	let uniEmitEventName = ''; // 确定时将数据通过 uni.$emit(uniEmitEventName, data); 返回
	let locationData = null;
	let mapCtx = null;

	const address = ref('');
	const location = ref({ // 初始化经纬度
		latitude: 39.918269,
		longitude: 116.396885,
	});

	onLoad((options) => {
		if (!options.emit) return uni.showNoneToast('缺少参数');
		uniEmitEventName = options.emit;

		uni.getLocation({
			type: 'gcj02',
			success: (res) => {
				location.value.latitude = res.latitude;
				location.value.longitude = res.longitude;
				getlnglatAddress(res.longitude, res.latitude);
			}
		});
	});

	onMounted(() => {
		mapCtx = uni.createMapContext("map", getCurrentInstance());
	});

	// 视野变化时
	const regionchange = () => {
		mapCtx.getCenterLocation({
			success: (res) => {
				getlnglatAddress(res.longitude, res.latitude);
			}
		})
	}

	// 获取地址
	const getlnglatAddress = async (lng, lat) => {
		const location = `${parseFloat((lng).toFixed(6))},${parseFloat((lat).toFixed(6))}`;
		const res = await api.user_api.getLngLatAddress({ location });
		locationData = res.data.data;
		address.value = res.data.data.formatted_address;
	}

	// 确定
	const handleSave = () => {
		const province = locationData.addressComponent.province;
		const city = typeof locationData.addressComponent.city === 'string' ? locationData.addressComponent.city: '';
		const area = locationData.addressComponent.district;
		const address = locationData.formatted_address.split(province + city + area)[1];
		const location = {
			province: province.endsWith('市') ? province.slice(0, -1): province,
			city: city || province,
			area,
			address
		}
		uni.$emit(uniEmitEventName, location);
		uni.navigateBack();
	}
</script>

<style lang="scss" scoped>
	.page {
		display: flex;
		flex-direction: column;
		height: 100vh;

		.map-box {
			position: relative;
			width: 100vw;
			height: 100vh;

			.map {
				width: 100%;
				height: 100%;
			}

			.mark {
				position: absolute;
				left: 50%;
				bottom: 50%;
				transform: translateX(-50%);
				z-index: 99;
				min-width: 80vw;
				text-align: center;

				.label {
					max-width: 80vw;
					display: inline-block;
					font-size: 30rpx;
					color: white;
					padding: 10rpx 20rpx;
					line-height: 1.5;
					// white-space: nowrap;
					background-color: rgba(0, 0, 0, 0.8);
					border-radius: 10rpx;
				}

				.location-icon {
					display: block;
					width: 60rpx;
					height: 60rpx;
					margin: 20rpx auto 0;
				}
			}

		}

		.btn-box {
			position: absolute;
			left: 0;
			bottom: 0;
			z-index: 99;
			width: 100%;
			padding: 40rpx 45rpx;
			box-sizing: border-box;

			.submit-btn {
				height: 88rpx;
				border-radius: 44rpx;
				font-size: 28rpx;
				color: white;
				text-align: center;
				line-height: 88rpx;
				background-color: #FE3333;
			}
		}
	}
</style>